#include "stdio.h"
#include "SDL/SDL.h"
     struct pion
{
    int couleur;
    SDL_Rect pos;

};


 int jouer(){
        SDL_Surface *screen;
    SDL_Rect src,dest;
    SDL_Surface *image;
    SDL_Surface *im1;
    SDL_Surface *im0;
    int i,j;
    typedef struct pion pion;
//initialisation de la matrice
   pion matricepion[10][10]={{{-1,{0,0,0,0}},{1,{50,0,0,0}},{-1,{100,0,0,0}},{1,{150,0,0,0}},{-1,{200,0,0,0}},{1,{250,0,0,0}},{-1,{300,0,0,0}},{1,{350,0,0,0}},{-1,{400,0,0,0}},{1,{450,0,0,0}}},
    {{1,{0,50,0,0}},{-1,{50,50,0,0}},{1,{100,50,0,0}},{-1,{150,50,0,0}},{1,{200,50,0,0}},{-1,{250,50,0,0}},{1,{300,50,0,0}},{-1,{350,50,0,0}},{1,{400,50,0,0}},{-1,{450,50,0,0}}},
    {{-1,{0,100,0,0}},{1,{50,100,0,0}},{-1,{100,100,0,0}},{1,{150,100,0,0}},{-1,{200,100,0,0}},{1,{250,100,0,0}},{-1,{300,100,0,0}},{1,{350,100,0,0}},{-1,{400,100,0,0}},{1,{450,100,0,0}}},
    {{1,{0,150,0,0}},{-1,{50,150,0,0}},{1,{100,150,0,0}},{-1,{150,150,0,0}},{1,{200,150,0,0}},{-1,{250,150,0,0}},{1,{300,150,0,0}},{-1,{350,150,0,0}},{1,{400,150,0,0}},{-1,{450,150,0,0}}},
    {{-1,{0,200,0,0}},{-1,{50,200,0,0}},{-1,{100,200,0,0}},{-1,{150,200,0,0}},{-1,{200,200,0,0}},{-1,{250,200,0,0}},{-1,{300,200,0,0}},{-1,{350,200,0,0}},{-1,{400,200,0,0}},{-1,{450,200,0,0}}},
    {{-1,{0,250,0,0}},{-1,{50,250,0,0}},{-1,{100,250,0,0}},{-1,{150,250,0,0}},{-1,{200,250,0,0}},{-1,{250,250,0,0}},{-1,{300,250,0,0}},{-1,{350,250,0,0}},{-1,{400,250,0,0}},{-1,{450,250,0,0}}},
    {{-1,{0,300,0,0}},{0,{50,300,0,0}},{-1,{100,300,0,0}},{0,{150,300,0,0}},{-1,{200,300,0,0}},{0,{250,300,0,0}},{-1,{300,300,0,0}},{0,{350,300,0,0}},{-1,{400,300,0,0}},{0,{450,300,0,0}}},
    {{0,{0,350,0,0}},{-1,{50,350,0,0}},{0,{100,350,0,0}},{-1,{150,350,0,0}},{0,{200,350,0,0}},{-1,{250,350,0,0}},{0,{300,350,0,0}},{-1,{350,350,0,0}},{0,{400,350,0,0}},{-1,{450,350,0,0}}},
    {{-1,{0,400,0,0}},{0,{50,400,0,0}},{-1,{100,400,0,0}},{0,{150,400,0,0}},{-1,{200,400,0,0}},{0,{250,400,0,0}},{-1,{300,400,0,0}},{0,{350,400,0,0}},{-1,{400,400,0,0}},{0,{450,400,0,0}}},
    {{0,{0,450,0,0}},{-1,{50,450,0,0}},{0,{100,450,0,0}},{-1,{150,450,0,0}},{0,{200,450,0,0}},{-1,{250,450,0,0}},{0,{300,450,0,0}},{-1,{350,450,0,0}},{0,{400,450,0,0}},{-1,{450,450,0,0}}}};
//chargement de l'image
    image = SDL_LoadBMP("img/image.bmp");
    im1 = SDL_LoadBMP("img/im1.bmp");
    im0 = SDL_LoadBMP("img/im0.bmp");
    SDL_SetColorKey(im1, SDL_SRCCOLORKEY, SDL_MapRGB(im1->format, 255, 255, 255));
    SDL_SetColorKey(im0, SDL_SRCCOLORKEY, SDL_MapRGB(im0->format, 255, 255, 255));

//affichage du plateau de jeu et pions
screen = SDL_SetVideoMode(500,500, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
src.x = 0;
src.y = 0;
src.w = image->w;
src.h = image->h;
int done=1;
dest.x = 0;
dest.y = 0;
dest.w = image->w;
dest.h = image->h;

while(done){
SDL_BlitSurface(image, &src, screen, &dest);

for(i=0;i<10;i++){
    for(j=0;j<10;j++){
    if(matricepion[i][j].couleur==1){
    SDL_BlitSurface(im1, NULL, screen, &matricepion[i][j].pos);
    }
    if(matricepion[i][j].couleur==0){
    SDL_BlitSurface(im0, NULL, screen, &matricepion[i][j].pos);
    }
    }
}

    SDL_Flip(screen);}
//Release the surface
    SDL_FreeSurface(image);
    SDL_FreeSurface(im1);
    SDL_FreeSurface(im0);
    return(0);

    }

int Regles()
{
SDL_Surface *screen;
SDL_Surface *temp;
SDL_Surface *image;
SDL_Rect posO;
int Done=1;
SDL_Event event;
    screen = SDL_SetVideoMode(643, 654, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    temp = SDL_LoadBMP("img/Regle.bmp");
    image = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    posO.x = 0;
    posO.y = 0;
    posO.w = image->w;
    posO.h = image->h;

    while(Done)
    {

        SDL_BlitSurface(image,NULL , screen, &posO);
        SDL_Flip(screen);
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                Done = 0;
                break;
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    Done = 0;
                break;
            }
            }
        }

    }
    return 0;
}

int main(int argc,char **argv)
{
    SDL_Surface *temp;
SDL_Surface *image1;
    SDL_Surface *Fond;
    SDL_Rect PosF,posS;
    int Done=1;
    SDL_Surface *screen;
    SDL_Event event;

    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    screen = SDL_SetVideoMode(551, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));


 temp = SDL_LoadBMP("img/Menu.bmp");
    Fond = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(Fond, SDL_SRCCOLORKEY, SDL_MapRGB(Fond->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/Selct.bmp");
    image1 = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(image1, SDL_SRCCOLORKEY, SDL_MapRGB(image1->format, 255, 255, 255));


    PosF.x = 0;
    PosF.y = 0;
    PosF.w = Fond->w;
    PosF.h = Fond->h;

    posS.x = 0;
    posS.y = 20;
    posS.w = image1->w;
    posS.h = image1->h;

    while(Done)
    {

        screen = SDL_SetVideoMode(364, 364, 32, SDL_HWSURFACE| SDL_DOUBLEBUF);
        if (screen == NULL)
        {
            printf("Unable to set video mode: %s\n", SDL_GetError());
            return 1;
        }
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));
        SDL_BlitSurface(Fond, NULL, screen, &PosF);
        SDL_BlitSurface(image1,NULL , screen, &posS);
        SDL_Flip(screen);

        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                Done = 0;
                break;
            case SDL_MOUSEMOTION :
                if( (event.motion.x >=0) && (event.motion.x <=300) && (event.motion.y <=120)&& (event.motion.y >=0)) posS.y = 0;
                if( (event.motion.x >=0) && (event.motion.x <=300) && (event.motion.y <=120)&& (event.motion.y >=60)) posS.y = 40;
                if( (event.motion.x >=0) && (event.motion.x <=300) && (event.motion.y <=180)&& (event.motion.y >=120)) posS.y = 90;
                if( (event.motion.x >=0) && (event.motion.x <=300) && (event.motion.y <=240)&& (event.motion.y >=180)) posS.y = 140;
                break;
            case SDL_MOUSEBUTTONUP:
                if( (event.motion.x >=0) && (event.motion.x <=300) && (event.motion.y <=60)&& (event.motion.y >=0))
                {
                    jouer();
                }
                if( (event.motion.x >=0) && (event.motion.x <=300) && (event.motion.y <=120)&& (event.motion.y >=40))
                {}
                if( (event.motion.x >=0) && (event.motion.x <=300) && (event.motion.y <=180)&& (event.motion.y >=90)) {Regles();}
                if( (event.motion.x >=0) && (event.motion.x <=300) && (event.motion.y <=240)&& (event.motion.y >=140))Done=0;
                break;
            }
        }
    }
    return(0);
}
